package com.wzh.glsc.order.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaoming
 * @ClassName:@{NAME}
 * @Description:
 * @date:@{DATE}@{TIME}
 */
//在调用的类没加Component时 可以开启属性配置
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {
    //创建一个新的线程
    //ThreadPoolConfigProperties加 Component 时 可以直接调用
    //这里创建了一个核心线程自定义、线程大小自定义、线程空闲时间自定义 秒
    //       阻塞队列为10W、创建线程工厂为默认工厂、拒绝策略为抛弃策略的线程池
//    @Primary
    @Bean //注入到容器中
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties){
        return new ThreadPoolExecutor(
                threadPoolConfigProperties.getCoreSize(),
                threadPoolConfigProperties.getMaxSize(),
                threadPoolConfigProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,new LinkedBlockingQueue<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

}
